package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.wgt.test.bean.Message;
import com.stu.wgt.test.mapper.MessageMapper;
import com.stu.wgt.test.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-01-25
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    /**
     * 添加
     *
     * @param message
     * @return 添加的行数
     */
    @Override
    public int insert(Message message) {
        return messageMapper.insert(message);
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(int id) {
        return messageMapper.deleteById(id);
    }

    /**
     * 更新message
     *
     * @param message
     * @return
     */
    @Override
    public int update(Message message) {
        QueryWrapper<Message> qw = new QueryWrapper<>();
        qw.eq("id", message.getId());
        return messageMapper.update(message, qw);
    }

    /**
     * 查询全部的信息
     *
     * @return
     */
    @Override
    public List<Message> selectAllMessage() {
        return messageMapper.selectList(null);
    }

    /**
     * 根据ID查询信息
     *
     * @param id
     * @return
     */
    @Override
    public List<Message> selectById(int id) {
        QueryWrapper<Message> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return messageMapper.selectList(qw);
    }

    @Override
    public List<Message> selectByName(String username) {
        return null;
    }

    /**
     * 分页搜索
     *
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Message> searchMessage(Integer userId, String query, int pageNum, int pageSize) {
        Page<Message> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Message> qw = new QueryWrapper<>();
        if(query != null && !query.isEmpty()) {
            qw.eq("category", query);
        }
        if(userId != null) {
            qw.eq("userId", userId);
        }
        IPage<Message> iPage = messageMapper.selectPage(page, qw);
        return iPage;
    }
}
